-- Leviathan (c) v.v.balashoff
local Class = require('leviathan/Class')
local i18n = require('libs/i18n')
local AudioManager = require('leviathan/Managers/AudioManager')
local FontsManager = require('leviathan/Managers/FontsManager')
local ImagesManager = require('leviathan/Managers/ImagesManager')
local Log = require('leviathan/Log')
local Settings = require('leviathan/settings')
local World = require('leviathan/ECS/World')

local Engine = Class:derive('Engine')

-- TODO DEBUG
dofile('libs/print_r.lua')

local time = os.date("*t")
math.randomseed(os.time())

-- TODO UGLY
dofile('src/script.lua')

-- global array for current settings
Config = {} -- TODO UGLY!
world = {}

function Engine:new()
    Log:info('[Leviathan] Starting...')

    -- set screen size
    -- TODO move it somewhere
    local currX, currY, cflags = love.window.getMode()
    local modes = love.window.getFullscreenModes()
    table.sort(modes, function(a, b) return a.width * a.height > b.width * b.height end)
    -- check screen size to fit to screen
    local width, height
    width = modes[1].width
    height = modes[1].height
    if (currX > width) or (currY > height) then
        width = width - 80
        height = height - 80
        local s = '[Leviathan] Set screen size to fit to screen: '..currX..'x'..currY..' -> '..width..'x'..height
        Log:warning(s)
        love.window.setMode(width, height, {vsync = false})
    else
        Log:info('[Leviathan] Game window resolution is: '..width..'x'..height)
    end

    -- TODO git
    --local version = ''
    --local command = 'git describe'
    --local handle = io.popen(command)
    --local version = handle:read('*a')
    --print(version)
    -- get git commit hash
    --local command = 'git rev-parse --short HEAD'
    --local handle = io.popen(command)
    --local result = handle:read('*a')
    --print(result)
    --handle:close()

    -- loading default settings
--    ENGINE:setDefaults('src/defaults')

    -- settings file
    Settings:LoadSettings()

    -- set locale
    local loc = 'i18n/'..Config.Locale..'.lua'
    Log:info('[Leviathan] Loading i18n file for language: '..Config.Locale)
    i18n.loadFile(loc)
    i18n.setLocale(Config.Locale)
    Log:info('[Leviathan] Current language: '..i18n.getLocale())
    -- set window title
    love.window.setTitle(i18n.translate('ProjectName'))

    -- TODO move it to global?
    world = World()
    -- TODO переделать это
    -- adding managers
    world.mgr = {}
    world.mgr.FontsManager = FontsManager()
    world.mgr.ImagesManager = ImagesManager()
    world.mgr.AudioManager = AudioManager()

    -- TODO
    -- creating domains
    local inp = world:getDomain('Input')
--    local core = world:getDomain('Core')
--    local script = world:getDomain('Script')
    local audio = world:getDomain('Audio')
    local fps = world:getDomain('FPS')
    -- starting gameplay
    local domain = nil
    if Config.ShowIntro then
        domain = 'Preface'
    else
        domain = 'MainMenu'
    end
--domain = 'About'
    world:switchDomain(nil, domain)
end

function Engine:update(dt)
    world:update(dt)
end

function Engine:draw()
    world:draw()
end

function Engine:quit()
    Log:info('[Leviathan] Exiting...')
    -- reset cursor to default one
    love.mouse.setCursor()
    Log:info('[Leviathan] Finished. Bye!')
end

return Engine
